package com.bysj.lms.controller;


import com.bysj.lms.common.ResultModel;
import com.bysj.lms.pojo.entity.Room;
import com.bysj.lms.service.ILmsFaService;
import com.bysj.lms.service.ILmsLveService;
import com.bysj.lms.service.IRoomService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaojiakai
 * @since 2022-03-08
 */
@Api(tags = "实验室管理")
@RestController
@RequestMapping("/room")
public class RoomController {

    @Autowired
    IRoomService roomService;

    @Autowired
    ILmsFaService lmsFaService;
    @Autowired
    ILmsLveService lmsLveService;

    @ApiOperation(value = "实验室模型")
    @GetMapping("/getModel")
    public Room getModel(){
        return new Room();
    }

    @ApiOperation(value = "查看实验室")
    @GetMapping("/find")
    public ResultModel find(String name,Integer page,Integer count){
        PageInfo pageInfo = roomService.findByPage(name, page, count);
        return ResultModel.success("查找成功",pageInfo);
    }

    @ApiOperation(value = "添加实验室")
    @PutMapping("/add")
    public ResultModel add(@RequestBody Room room){
        boolean save = roomService.save(room);
        if(save) return ResultModel.success("添加成功");
        return ResultModel.failure("添加失败");
    }
    @ApiOperation(value = "根据id删除")
    @DeleteMapping("/delete")
    public ResultModel delete(Integer id){
        boolean remove = roomService.removeById(id);
        if(remove) return ResultModel.success("删除成功");
        return ResultModel.failure("删除失败");
    }

    @ApiOperation("修改")
    @PutMapping("/update")
    public ResultModel update(@RequestBody Room room){
        boolean update = roomService.updateById(room);
        if(update) return ResultModel.success("更新成功");
        return ResultModel.failure("更新失败");
    }

    @ApiOperation("批量配置设备")
    @PutMapping("/allocation")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "type",value = "设备类型,0固定，1低值"),
//            @ApiImplicitParam(name = "numbers",value = "设备编号"),
//            @ApiImplicitParam(name = "roomId",value = "实验室id"),
//    })
    public ResultModel allocation(@RequestBody Map<String,Object> map){
        boolean binds = true;
        Integer type = (Integer) map.get("type");
        ArrayList<String> numbers = (ArrayList<String>) map.get("numbers");
        String roomId = (String) map.get("roomId");
        if(type ==0){
            binds = lmsFaService.binds(numbers, roomId);
        }else if(type==1){
            binds = lmsLveService.binds(numbers,roomId);
        }
        return binds?ResultModel.success():ResultModel.failure();
    }
}

